<?php
class Marticle extends CI_Model{
	protected $_table="tpt_article";
    protected $_table_1="tpt_article_content";
	protected $_table_2="tpt_catalogue";
	protected $_table_3="tpt_section";
	public function __construct(){
		parent::__construct();	
		$this->load->database();
        $this->load->library('datatables');
	} 	

	public function listArticle(){
        $sTable = $this->_table;
        $sTable_1 = $this->_table_1;
        $sTable_2 = $this->_table_2;
        $sTable_3 = $this->_table_3;

        $this->datatables
            ->select('a.id as id, a.author as author, a.check as check, a.created_at as created_at')
            ->from($sTable.' as a')
            ->join($sTable_1.' as b', 'a.id = b.art_id','left')
            ->select('b.title as art_title')
            ->join($sTable_2.' as c', 'a.cata_id = c.id','left')
            ->select('c.title as cata_title')
            ->join($sTable_3.' as d', 'a.sec_id = d.id','left')
            ->select('d.title as sec_title')
            ->where('b.lang_id',1);
        add_column_datatable('id', TRUE, TRUE, FALSE);
        return $this->datatables->generate();
    }
	
	public function insert($data){
		$this->db->insert($this->_table,$data);	
	}

	public function get_data($id){
        $dataLang = $this->registry->get('language');
        $maxLang = count($dataLang);
        $selectBeforeFrom = '';
        $selectAfterFrom = '';
        $where = 'b.id = '.$id;
        $i=1;
        foreach($dataLang as $lang){
            $i_next = $i + 1;
            if($i == 1){
                $selectBeforeFrom .= 'a1.art_id, b.sec_id, b.cata_id, b.tag_id, a1.title as art_title_'.$lang['lang_code'].', a1.info as art_info_'.$lang['lang_code'].', a1.content as art_content_'.$lang['lang_code'];
                $selectAfterFrom .= '(select * from tpt_article) b ,';
                $selectAfterFrom .= '(select * from tpt_article_content where lang_id = "'.$lang['id'].'") a1';
            }else{
                $selectBeforeFrom .= ', a'.$i.'.title as art_title_'.$lang['lang_code'].', a'.$i.'.info as art_info_'.$lang['lang_code'].', a'.$i.'.content as art_content_'.$lang['lang_code'];    
                $selectAfterFrom .= ', (select * from tpt_article_content where lang_id = "'.$lang['id'].'") a'.$i;
                if($i == $maxLang){
                    $i_next = 1;
                }
            }
            $where .= ' AND a'.$i.'.art_id = b.id';
            $i++;
        }
        $sql = "select SQL_CALC_FOUND_ROWS ".$selectBeforeFrom." from ".$selectAfterFrom."
where ".$where." ORDER BY a1.id DESC";
        return $this->db->query($sql)->row_array();
	}

	public function delete($id){
		$this->db->where("id",$id);
		$this->db->delete($this->_table);
	}

	public function update($data,$id){
		$this->db->where("id",$id);
		$this->db->update($this->_table,$data);
	}
	
	public function update_view($url){
		$this->db->set('view', 'view + 1', FALSE);
		$this->db->where("url",$url);
		$this->db->update($this->_table);
	}
}